<?php
namespace App\WxPay;


/**
 * 红包接口类
 */
class Sendredpack_pub extends Wxpay_client_pub
{
    function __construct()
    {
        //设置接口链接
        $this->url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
        //设置curl超时时间
        $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
    }

    /**
     * 生成接口参数xml
     */
    function createXml()
    {
        try
        {
            //检测必填参数
            if($this->parameters["mch_billno"] == null)
            {
                throw new SDKRuntimeException("缺少统一支付接口必填参数mch_billno！"."<br>");
            }elseif($this->parameters["wishing"] == null){
                throw new SDKRuntimeException("缺少统一支付接口必填参数wishing！"."<br>");
            }elseif ($this->parameters["total_amount"] == null ) {
                throw new SDKRuntimeException("缺少统一支付接口必填参数total_amount！"."<br>");
            }elseif ($this->parameters["re_openid"] == null) {
                throw new SDKRuntimeException("缺少统一支付接口必填参数openid！"."<br>");
            }elseif ($this->parameters["act_name"] == null) {
                throw new SDKRuntimeException("缺少统一支付接口必填参数act_name！"."<br>");
            }elseif ($this->parameters["remark"] == null) {
                throw new SDKRuntimeException("缺少统一支付接口必填参数remark！"."<br>");
            }
            $this->parameters["wxappid"] = WxPayConf_pub::APPID;//公众账号ID
            $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
            $this->parameters["total_num"] = 1;//红包发放总人数
            $this->parameters["send_name"] = "微知乐";//红包发送者名称
            $this->parameters["client_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
            $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
            $this->parameters["sign"] = $this->getSign($this->parameters);//签名
            return  $this->arrayToXml($this->parameters);
        }catch (SDKRuntimeException $e)
        {
            die($e->errorMessage());
        }
    }

    /**
     * 	作用：获取结果，使用证书通信
     */
    function getResult()
    {
        $this->postXmlSSL();
        $this->result = $this->xmlToArray($this->response);
        return $this->result;
    }

}
